﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using Jalal.Library.Utility;
using BCGC.BLL.BusinessObject.Resource;
using BCGC.BLL.BusinessObject;
using System.IO;
using System.Drawing;
using System.Collections.Generic;

public partial class Admin_Resource_Trailer_EditTrailer : AdminBasePage
{
    private string PreviewImageUrl
    {
        get { return (string)ViewState["PreviewImageUrl"]; }
        set { ViewState["PreviewImageUrl"] = value; }
    }

    private string ThumbnailImageUrl
    {
        get { return (string)ViewState["ThumbnailImageUrl"]; }
        set { ViewState["ThumbnailImageUrl"] = value; }
    }

    private int Id
    {
        get
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"];
        }
        set { ViewState["Id"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
                LoadData();
            }
            else
                Response.Redirect(CustomSiteMap.Trailers);
        }
    }

    private void LoadData()
    {

        ddlGameTitle.DataSource = Game.GetAllGames();
        ddlGameTitle.DataTextField = "GameTitle";
        ddlGameTitle.DataValueField = "GameId";
        ddlGameTitle.DataBind();

        ddlGameTitle.Items.Add(new ListItem("Create New Game", "NewGame"));
        ddlGameTitle.Items.Insert(0, new ListItem("Select Game", "-1"));
        ddlGameTitle.SelectedIndex = 0;

        pnlAddGame.Visible = false;
        tbxNewGame.Text = string.Empty;

        Trailer trailer = Trailer.GetTrailerById(Id);
        if (trailer == null)
            Response.Redirect(CustomSiteMap.Trailers);
        else
        {
            FileInfo fileInfo = new FileInfo(Server.MapPath(trailer.FileUrl));

            tbxTitle.Text = trailer.Title;
            ddlGameTitle.SelectedValue = trailer.RelatedGame.GameId.ToString();
            hlkDownloadTrailerFile.Text = fileInfo.Name;
            hlkDownloadTrailerFile.NavigateUrl = trailer.FileUrl;
            fckDescription.Value = Server.HtmlDecode(trailer.Description);
            cbxActive.Checked = trailer.IsActive;
            imgVideoPreview.ImageUrl = PreviewImageUrl = trailer.PreviewImageUrl;
            imgVideoPreview.AlternateText = "Preview Image";
            imgVideoThumbnail.ImageUrl = ThumbnailImageUrl = trailer.ThumbnailImageUrl;
            imgVideoThumbnail.AlternateText = "Thubmnail Image";
            tbxWidth.Text = trailer.Width.ToString();
            tbxHeight.Text = trailer.Height.ToString();
        }
    }

    protected void AddNewGame_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (Game.Create(Security.BindStringInput(tbxNewGame.Text, SecurityLevel.Three)))
            {
                uccResponse.SetSuccessMessage("Game successfuly created");
                LoadData();
            }
            else
                uccResponse.SetFailMessage("Failed to create game");
        }
    }

    protected void GameTitle_Changed(object sender, EventArgs e)
    {
        if (ddlGameTitle.SelectedValue == "NewGame")
            pnlAddGame.Visible = true;
        else
            pnlAddGame.Visible = false;
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            UploadTrailerImageFile();
            if (Trailer.Update(Id, Security.BindStringInput(tbxTitle.Text, SecurityLevel.Three),
                Security.BindIntegerInput(ddlGameTitle.SelectedValue, SecurityLevel.Three),
                Security.BindStringInput(fckDescription.Value, SecurityLevel.Three),
                Security.BindStringInput(UploadTrailerFile(), SecurityLevel.Three),
                Security.BindStringInput(ThumbnailImageUrl, SecurityLevel.Three),
                Security.BindStringInput(PreviewImageUrl, SecurityLevel.Three),
                Security.BindIntegerInput(tbxWidth.Text, SecurityLevel.Three),
                Security.BindIntegerInput(tbxHeight.Text, SecurityLevel.Three),
                cbxActive.Checked))
            {
                LoadData();
                uccResponse.SetSuccessMessage("Trailer successfuly updated");
            }
            else
                uccResponse.SetFailMessage("Failed to update trailer");
        }
    }

    private void UploadTrailerImageFile()
    {
        if (uccImageUploader.HasFile)
        {
            List<Size> sizes = new List<Size>();
            sizes.Add(new Size(100, 0));
            sizes.Add(new Size(int.Parse(tbxWidth.Text.Trim()), 0));

            uccImageUploader.Sizes = sizes;
            List<string> uploadImagePaths = uccImageUploader.UploadImage();
            if (uploadImagePaths != null && uploadImagePaths.Count > 1)
            {
                ThumbnailImageUrl = uploadImagePaths[0];
                PreviewImageUrl = uploadImagePaths[1];
            }
        }
    }

    private string UploadTrailerFile()
    {
        if (fluTrailerFile.HasFile)
        {
            return Trailer.UploadTrailerFile(fluTrailerFile.PostedFile);
        }
        else
            return hlkDownloadTrailerFile.NavigateUrl;
    }
}
